package com.lrkj.back.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lrkj.back.entity.*;
import com.lrkj.back.mapper.ZsHuishouMingxiMapper;
import com.lrkj.back.mapper.ZsWanglanpackageMapper;
import com.lrkj.back.mapper.ZsHuishouQueshiMapper;
import com.lrkj.back.mapper.TbDepartmentMapper;
import com.lrkj.back.mapper.TbPackageMubanMapper;
import com.lrkj.back.mapper.ZsHuishouMapper;
import com.lrkj.back.mapper.ZsWanglanMapper;
import com.lrkj.back.service.ZsHuishouWutiaomaService;
import com.lrkj.back.service.ZsQingxiService;
import com.lrkj.back.vo.constant.PathInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lrkj.back.mapper.TbProductMapper;
import com.lrkj.back.mapper.ZsPackageImformationMapper;
import com.lrkj.back.service.TbSeqService;
import cn.com.cbons.mamisay.parent.exception.DBException;

@Transactional
@Service
public class ZsHuishouWutiaomaServiceImpl implements ZsHuishouWutiaomaService {

    @Autowired
    private TbDepartmentMapper tbdeptMapper;

    @Autowired
    private TbProductMapper tbProductMapper;

    @Autowired
    private TbPackageMubanMapper tbPackageMapper;

    @Autowired
    private ZsHuishouMapper zsHuishouMapper;

    @Autowired
    private ZsHuishouMingxiMapper zsHuishouMingxiMapper;

    @Autowired
    private TbSeqService seqService;

    @Autowired
    private ZsHuishouQueshiMapper zsHuishouQueshiMapper;

    @Autowired
    private ZsWanglanMapper zswanglanMapper;
    @Autowired
    private ZsPackageImformationMapper infoMapper;
    @Autowired
    private ZsWanglanpackageMapper zsWlpackMapper;

    @Autowired
    private ZsQingxiService zsQingxiService;

    /**
     * 回收方法
     *
     * @param huishou
     * @return
     */
    public String huishouadd(ZsHuishou huishou) throws DBException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String proId = "HS" + sdf.format(new Date()) + seqService.seqVal("huishouno");
        huishou.setHuishouLiushui(proId);
        huishou.setHuishouTime(new Date());
        huishou.setHuishouZhuangtai(0);
        zsHuishouMapper.insertSelective(huishou);
        return proId;
    }

    public void packageIMadd() {

    }

    @Override
    public int huishou(ZsHuishou zsHuishou, String[] depId, String[] packageId, String[] packageNum, String[] packageName,
                       String hswlTiaomas, String hswlMingchengs, Map<Integer, List<TbProduct>> map) throws DBException {
        try {
            Date dt = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String packageTiaoma = "";
            String hsliushui = huishouadd(zsHuishou);
            String hswlTiaoma = hswlTiaomas;
            for (int i = 0; i < packageId.length; i++) {
                int num = Integer.parseInt(packageNum[i]);
                String packId = packageId[i];
                Integer packageDep = Integer.parseInt(depId[i]);
                TbPackageMuban packageMuban = tbPackageMapper.selectByPrimaryKey(packId);

                String huishouBaoname = packageName[i];
                String hswlMingcheng = hswlMingchengs;
                Long[] packtmArr = seqService.seqValBatch("packagetiaomano", num);
                Long[] hsmxlsArr = seqService.seqValBatch("huishouMingxiLiushui", num);
                Long[] wlpackArr = seqService.seqValBatch("wlpackno", num);
                List<ZsHuishouMingxi> hsmxList = new ArrayList<>();
                List<ZsPackageImformation> packIm = new ArrayList<>();
                List<ZsWanglanpackage> wlpackList = new ArrayList<>();
                for (int j = 0; j < num; j++) {
                    packageTiaoma = "R" + sdf.format(dt) + packtmArr[j];
                    String hsmxlId = "HSMX" + sdf.format(dt) + hsmxlsArr[j];
                    //吗卖逼的代码坑死了
                    String wlpackId = sdf.format(dt)+String.valueOf(wlpackArr[j]);
                    TbPackageMuban tbPackageMuban = null;

//                    if (packageMuban != null) {
//                        Integer iiiInteger = packageMuban.getPackageDepartid();
//                        if (iiiInteger == null) {
//                            packageTiaoma = packageTiaoma.replace("R", "C");
//                        }
//                    }
                    packageImadd(zsHuishou, null, packageDep, dt, packageTiaoma, hsliushui, packIm, null, Integer.valueOf(packId), huishouBaoname,false);
                    //添加回收明细list的方法
                    huishouMingxiadd(packageTiaoma, dt, String.valueOf(packageDep), hswlTiaoma, hsmxlId, packageTiaoma, hsliushui, num, hsmxList, j, Integer.valueOf(packId), huishouBaoname);

                    wlpackadd(hswlTiaoma, hswlMingcheng, dt, wlpackId, packageTiaoma, packId, huishouBaoname, wlpackList,null,false);


                }
                zsHuishouMingxiMapper.insertBatch(hsmxList);
                infoMapper.insertBatch(packIm);
                zsWlpackMapper.insertBatch(wlpackList);
                queshiadd(zsHuishou, map, dt, sdf, packageTiaoma, packId);
            }
            // 变更网篮的状态
            ZsWanglan wl = new ZsWanglan();
            wl.setWlState(1);
            wl.setWlTiaoma(hswlTiaoma);
//            List<ZsWanglan> zsWanglanList = new ArrayList<>();
//            for (int i = 0; i < hswlTiaomas.length; i++) {
//                ZsWanglan zsWanglan = new ZsWanglan();
//                zsWanglan.setWlState(1);
//                zsWanglan.setWlTiaoma(hswlTiaomas[i]);
//                zsWanglanList.add(zsWanglan);
//            }
            zswanglanMapper.updateByPrimaryKeySelective(wl);
//            zswanglanMapper.updateBatch(zsWanglanList);

            return 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }

    }

    public void queshiadd(ZsHuishou zsHuishou, Map<Integer, List<TbProduct>> map, Date dt, SimpleDateFormat sdf, String packageTiaoma, String packId) {
        ZsHuishouQueshi queshi;
        if (map != null) {
            // 保存明细
            if (map.get(packId) != null && map.get(packId).size() > 0) {
                int count = 0;
                List<TbProduct> listPro = map.get(packId);
                int size = listPro.size();
                Long[] queshiArr = seqService.seqValBatch("queshino", size);
                List<ZsHuishouQueshi> list = new ArrayList<>();
                for (TbProduct product : listPro) {
                    queshi = new ZsHuishouQueshi();
                    queshi.setHuishouQueshiLiushui(sdf.format(dt) + queshiArr[count]);
                    queshi.setHuishouWupinbaoTiaoma(packageTiaoma);
                    queshi.setHuishouWupinId(String.valueOf(product.getProductId()));
                    queshi.setHuishouWupinMingcheng(product.getProductName());
                    queshi.setHuishouWupinShuliang(product.getNum());
                    queshi.setHuishouProductsize(product.getProductSize());
                    queshi.setHuishouProductbarcode(product.getProductBarcode());
                    queshi.setHuishouQueshiHuishourenTiaoma(zsHuishou.getHuishouHuishourenTiaoma());
                    queshi.setHuishouTime(zsHuishou.getHuishouTime());
                    queshi.setHuishouBeizhu(zsHuishou.getHuishouBeizhu());
                    list.add(queshi);
                    count++;
                }
                try {
                    zsHuishouQueshiMapper.insertBatch(list);
                } catch (Exception e) {

                }

            }
        }
    }

    public void wlpackadd(String hswlTiaoma, String hswlMingcheng, Date dt, String wlpackId, String packageTiaoma, String packId, String huishouBaoname, List<ZsWanglanpackage> wlpackList,String qingxiLiushui,boolean cangjia) {
        ZsWanglanpackage wlpack;// 包对应网篮表

        wlpack = new ZsWanglanpackage();
        wlpack.setPackageTiaoma(packageTiaoma);
        wlpack.setWanglanTiaoma(hswlTiaoma);
        wlpack.setRecodeTime(dt);
        wlpack.setWanglanName(hswlMingcheng);
        wlpack.setPackageName(huishouBaoname);
        if(qingxiLiushui==null){
            wlpack.setPackageZhuangtai(0);
        }else{
            wlpack.setPackageZhuangtai(1);
        }
        if(cangjia){
            wlpack.setPackageZhuangtai(2);
        }

        wlpack.setPackageId(String.valueOf(packId));
        wlpack.setHuishouTime(dt);
        wlpack.setWlpackLiushui(wlpackId);
        wlpack.setQingxiLiushui(qingxiLiushui);
        wlpackList.add(wlpack);
    }


    public int peibaoSave(String[] packageId, String[] packageNum,String [] depId,String huishouren) {
        try {
            int sum = 0;
            for(int j= 0;j<packageNum.length;j++){
                sum +=Integer.valueOf(packageNum[j]);
            }
            ZsHuishou zsHuishou = new ZsHuishou();
            zsHuishou.setHuishouHuishourenTiaoma(huishouren);
            zsHuishou.setHuishouJiaojierenTiaoma(huishouren);
            String hsliushui = huishouadd(zsHuishou);
            Date dt = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            String proId = "QX" + sdf2.format(new Date()) + seqService.seqVal("qingxino");
            ZsQingxi zsQingxi = new ZsQingxi();

            zsQingxi.setQingxiLiushui(proId);
            zsQingxi.setQingxiShebeiTiaoma(String.valueOf(1000016));
            zsQingxi.setQingxiRuanjianTiaoma(String.valueOf(1000095));
            zsQingxi.setQingxiRuanjianName("手工清洗1");
            zsQingxi.setQingxiShebeiName("手动清洗1");
            zsQingxi.setRecordTime(dt);
            zsQingxi.setPackageNum(sum);
            zsQingxi.setQingxiHege(1);
            zsQingxi.setQingxiZhuangtai(2);
            zsQingxi.setPackageFlag(1);
            String packageTiaoma = "";
            for (int i = 0; i < packageId.length; i++) {
                int num = Integer.parseInt(packageNum[i]);
                String packId = packageId[i];
                Integer packageDep = Integer.valueOf(depId[i]);
                TbPackageMuban packageMuban = tbPackageMapper.selectByPrimaryKey(packId);
                String huishouBaoname = packageMuban.getPackageName();
                Long[] packtmArr = seqService.seqValBatch("packagetiaomano", num);
                Long[] wlpackArr = seqService.seqValBatch("wlpackno", num);
                Long[] hsmxlsArr = seqService.seqValBatch("huishouMingxiLiushui", num);

                List<ZsPackageImformation> packIm = new ArrayList<>();
                List<ZsWanglanpackage> wlpackList = new ArrayList<>();
                List<ZsHuishouMingxi> hsmxList = new ArrayList<>();
                for (int j = 0; j < num; j++) {
                    packageTiaoma = "R" + sdf.format(dt) + packtmArr[j];
                    String hsmxlId = "HSMX" + sdf.format(dt) + hsmxlsArr[j];
                    String wlpackId = sdf.format(dt)+String.valueOf(wlpackArr[j]);
                    huishouMingxiadd(packageTiaoma, dt, depId[i], null, hsmxlId, packageTiaoma, hsliushui, num, hsmxList, i, Integer.valueOf(packId), huishouBaoname);
                    packageImadd(null, null, packageDep, dt, packageTiaoma, null, packIm, null, Integer.valueOf(packId), huishouBaoname,false);
                    wlpackadd(null, null, dt, wlpackId, packageTiaoma, packId, huishouBaoname, wlpackList,proId,false);

                }
                infoMapper.insertBatch(packIm);
                zsWlpackMapper.insertBatch(wlpackList);
                zsHuishouMingxiMapper.insertBatch(hsmxList);
            }
            zsQingxiService.insert(zsQingxi);
            return 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int huishouTm(ZsHuishou zsHuishou, String[] packTm, String[] packageId, String[] packageName, String[] packageKeshi,
                         String[] hswlTiaoma, Map<Integer, List<TbProduct>> map) throws DBException {
        try {
            ZsHuishouQueshi queshi = null;
            Date dt = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");

            String packageTiaoma = "";

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
            String proId = "HS" + sdf1.format(new Date()) + seqService.seqVal("huishouno");
            zsHuishou.setHuishouLiushui(proId);
            zsHuishou.setHuishouTime(new Date());
            zsHuishou.setHuishouZhuangtai(0);
            zsHuishouMapper.insertSelective(zsHuishou);
            String hsliushui = proId;
            int num = packageId.length;
            List<ZsHuishouMingxi> hsmxList = new ArrayList<>();
            List<ZsPackageImformation> packIm = new ArrayList<>();
            List<ZsPackageImformation> updatePackIm = new ArrayList<>();
            List<ZsWanglanpackage> wlpackList = new ArrayList<>();
            Long[] packtmArr = seqService.seqValBatch("packagetiaomano", num);
            Long[] hsmxlsArr = seqService.seqValBatch("huishouMingxiLiushui", num);
            Long[] wlpackArr = seqService.seqValBatch("wlpackno", num);
            for (int i = 0; i < num; i++) {
                Integer packId = Integer.parseInt(packageId[i]);
                String huishouBaoname = packageName[i];
                //如果这个部门被删掉就有可能出现这个问题
                String depId = packageKeshi[i];
                Integer depId1 = null;
                if(StringUtils.isNotBlank(depId)&&!"null".equals(depId)){
                    depId1 = Integer.valueOf(depId);
                }
                String hswlTm = hswlTiaoma[i];
                String hsmxlId = "HSMX" + sdf.format(dt) + hsmxlsArr[i];
                String wlpackId = sdf.format(dt)+String.valueOf(wlpackArr[i]);
                packageTiaoma = "R" + sdf.format(dt) + packtmArr[i];
                TbPackageMuban tbPackageMuban = null;
                ZsPackageImformation zsPackageImformation = infoMapper.selectByPrimaryKey(packTm[i]);
                if(zsPackageImformation!=null){
                    tbPackageMuban = zsPackageImformation.getTbPackageMuban();
                }
                boolean wailai = false;
                if(tbPackageMuban!=null){
                    Integer integer = tbPackageMuban.getPackageOutcategory();
                    if(integer!=null&&integer==1){
                        if(zsPackageImformation.getPackageLiuchengZhuangtai()>=4){
                            wailai= true;
                            packageTiaoma = packTm[i];
                        }
                    }
                }
                //大表添加
                packageImadd(zsHuishou, packTm[i], depId1, dt, packageTiaoma, hsliushui, packIm, updatePackIm, packId, huishouBaoname,wailai);


                //回收明细添加
                huishouMingxiadd(packTm[i], dt, depId, hswlTm, hsmxlId, packageTiaoma, hsliushui, num, hsmxList, i, packId, huishouBaoname);
                //网篮 包关系表

                wlpackadd(hswlTm, null, dt, wlpackId, packageTiaoma, String.valueOf(packId), huishouBaoname, wlpackList,null,wailai);
                //缺失表
                queshiadd(zsHuishou, map, dt, sdf, packageTiaoma, String.valueOf(packId));

            }
            if (packIm.size() != 0) {
                infoMapper.insertBatch(packIm);
            }
//            if (updatePackIm.size() != 0) {
//                infoMapper.updateByBatch(updatePackIm);
//            }
            zsWlpackMapper.insertBatch(wlpackList);
            zsHuishouMingxiMapper.insertBatch(hsmxList);
            // 变更网篮的状态
            ZsWanglan wl = new ZsWanglan();
            wl.setWlState(1);
            for (int i = 0; i < hswlTiaoma.length; i++) {
                wl.setWlTiaoma(hswlTiaoma[i]);
                zswanglanMapper.updateByPrimaryKeySelective(wl);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    public String packageImadd(ZsHuishou zsHuishou, String s, Integer depId, Date dt, String packageTiaoma, String hsliushui, List<ZsPackageImformation> packIm, List<ZsPackageImformation> updatePackIm, Integer packId, String huishouBaoname,Boolean update) {
        ZsPackageImformation packageInfo;
        ZsPackageImformation updataPackageinfo;// 大表
        String huishouren = zsHuishou==null?null:zsHuishou.getHuishouHuishourenTiaoma();
        String jiaojieren = zsHuishou==null?null:zsHuishou.getHuishouJiaojierenTiaoma();
        ZsPackageImformation zsPackageImformation = null;
        if (!update) {
            packageInfo = new ZsPackageImformation();
            packageInfo.setPackageTiaoma(packageTiaoma);
            packageInfo.setPackageId(packId);
            packageInfo.setPackageName(huishouBaoname);
            packageInfo.setHuishouHuishourenTiaoma(huishouren);
            packageInfo.setHuishouJiaojierenTiaoma(jiaojieren);
            packageInfo.setHuishouAllzhuangtai(1);
            packageInfo.setHuishouTime(dt);
            packageInfo.setPackageLiuchengZhuangtai(1);
            packageInfo.setHuishouLiushui(hsliushui);
            packageInfo.setPackageKeshi(depId);
            packIm.add(packageInfo);
        } else {
//            updataPackageinfo = new ZsPackageImformation();
//            updataPackageinfo.setPackageTiaoma(s);
//            updataPackageinfo.setPackageId(packId);
//            updataPackageinfo.setPackageName(huishouBaoname);
//            updataPackageinfo.setHuishouHuishourenTiaoma(huishouren);
//            updataPackageinfo.setHuishouJiaojierenTiaoma(jiaojieren);
//            updataPackageinfo.setHuishouTime(dt);
//            updataPackageinfo.setHuishouLiushui(hsliushui);
//            updataPackageinfo.setPackageLiuchengZhuangtai(8);
//            updatePackIm.add(updataPackageinfo);
//            packageTiaoma = s;
//            return packageTiaoma;
        }
        return null;
    }

    public void huishouMingxiadd(String s, Date dt, String depId, String wlTM, String hsmxlId, String packageTiaoma, String hsliushui, int num, List<ZsHuishouMingxi> hsmxList, int i, Integer packId, String huishouBaoname) {
        ZsHuishouMingxi zsHuishouMingxi = new ZsHuishouMingxi();
        ;
        if (s.startsWith("C")) {
            zsHuishouMingxi.setHuishouWupinbaoTiaoma(s);
        } else if (s.startsWith("R")) {
            zsHuishouMingxi.setHuishouWupinbaoTiaoma(packageTiaoma);
        }
        zsHuishouMingxi.setHuishouLiushui(hsliushui);
        zsHuishouMingxi.setHuishouMingxiLiushui(hsmxlId);
        zsHuishouMingxi.setHuishouPeibaozhuangtai(0);
        zsHuishouMingxi.setHuishouWupinbaoMuban(packId);
        zsHuishouMingxi.setHuishouBaoname(huishouBaoname);
        zsHuishouMingxi.setHuishouTime(dt);
        zsHuishouMingxi.setHuishouWanglan(wlTM);
        zsHuishouMingxi.setHuishouKeshi(depId);
        hsmxList.add(zsHuishouMingxi);
    }

    @Override
    public int huishouAndroid(List<ZsPackageImformation> zsPackageImformations,String wlBarcode,
                              String empId, String huishouren) throws DBException {
        try {
            Date dt = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            ZsHuishou zsHuishou = new ZsHuishou();
            zsHuishou.setHuishouHuishourenTiaoma(huishouren);
            zsHuishou.setHuishouJiaojierenTiaoma(empId);
            String hsliushui = huishouadd(zsHuishou);
            List<ZsHuishouMingxi> hsmxList = new ArrayList<>();
            List<ZsPackageImformation> packIm = new ArrayList<>();
            List<ZsPackageImformation> updatePackIm = new ArrayList<>();
            List<ZsWanglanpackage> wlpackList = new ArrayList<>();
            int num = zsPackageImformations.size();
            Long[] packtmArr = seqService.seqValBatch("packagetiaomano", num);
            Long[] hsmxlsArr = seqService.seqValBatch("huishouMingxiLiushui", num);
            Long[] wlpackArr = seqService.seqValBatch("wlpackno", num);
            int count = 0;
            for (int i = 0; i < zsPackageImformations.size(); i++) {
                ZsPackageImformation zsPackageImformation = zsPackageImformations.get(i);
                Integer packId = zsPackageImformation.getPackageId();
                Integer keshi = zsPackageImformation.getKeshiId();
                String huishouBaoname = zsPackageImformation.getPackageName();
                String hswlTm = wlBarcode;
                String hsmxlId = "HSMX" + sdf.format(dt) + hsmxlsArr[count];
                String wlpackId = sdf.format(dt)+String.valueOf(wlpackArr[count]);
                String packageTiaoma = "R" + sdf.format(dt) + packtmArr[count];
                TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
                boolean wailai  = false;
                if(tbPackageMuban!=null){
                    Integer integer = tbPackageMuban.getPackageOutcategory();
                    if(integer!=null&&integer==1){
                        if(zsPackageImformation.getPackageLiuchengZhuangtai()>=4){
                            wailai = true;
                        }
                    }
                }
                if(wailai){
                    packageTiaoma = zsPackageImformation.getPackageTiaoma();
                }
                //大表添加
                packageImadd(zsHuishou, zsPackageImformation.getPackageTiaoma(), keshi, dt, packageTiaoma, hsliushui, packIm, updatePackIm, packId, huishouBaoname,wailai);
                huishouMingxiadd(zsPackageImformation.getPackageTiaoma(), dt, String.valueOf(keshi), hswlTm, hsmxlId, packageTiaoma, hsliushui, num, hsmxList, count, packId, huishouBaoname);
                //网篮 包关系表
                wlpackadd(hswlTm, null, dt, wlpackId, packageTiaoma, String.valueOf(packId), huishouBaoname, wlpackList,null,wailai);

            }
            ZsWanglan wl = new ZsWanglan();
            wl.setWlState(1);
            wl.setWlTiaoma(wlBarcode);
            zswanglanMapper.updateByPrimaryKeySelective(wl);

            if (packIm.size() != 0) {
                infoMapper.insertBatch(packIm);
            }
//            if (updatePackIm.size() != 0) {
//                infoMapper.updateByBatch(updatePackIm);
//            }
            if(wlpackList.size()!=0){
                zsWlpackMapper.insertBatch(wlpackList);
            }
            if(hsmxList!=null){
                zsHuishouMingxiMapper.insertBatch(hsmxList);
            }
            // 变更网篮的状态
            return 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
    }

}
